package ltd.dujiabao.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import ltd.dujiabao.commonutils.Result;
import ltd.dujiabao.eduservice.entity.EduCourse;
import ltd.dujiabao.eduservice.entity.EduTeacher;
import ltd.dujiabao.eduservice.service.EduCourseService;
import ltd.dujiabao.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : DuJiabao
 * @Project : online-course-project
 * @Time : 2021/4/24 13:56
 * @Desc : 处理前台的请求
 */
@Api(value = "讲师管理")
@RestController
@RequestMapping("/eduservice/front")
@CrossOrigin
@Slf4j
public class EduFrontController {
  @Autowired
  EduTeacherService eduTeacherService;
  @Autowired
  EduCourseService eduCourseService;

  @ApiOperation("前limit个讲师列表")
  @GetMapping("/teacher/{limit}")
  public Result getTeachersLimit(@PathVariable long limit) {
    QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
    wrapper.orderByDesc("id"); // 按照id降序排列
    wrapper.last("limit " + limit); // 前limit个
    List<EduTeacher> teachers = eduTeacherService.list(wrapper);

    log.info("前" + limit + "个 Teacher：" + teachers.toString());
    return Result.success().addData("data", teachers);
  }

  @ApiOperation("前limit个课程列表")
  @GetMapping("/course/{limit}")
  public Result getCoursesLimit(@PathVariable long limit) {
    QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
    wrapper.orderByDesc("id"); // 按照id降序排列
    wrapper.last("limit " + limit); // 前limit个
    List<EduCourse> courses = eduCourseService.list(wrapper);

    log.info("前" + limit + "个 Course：" + courses.toString());
    return Result.success().addData("data", courses);
  }

}
